package com.jamlu.framework.album;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jamlu.framework.GlideApp;
import com.jamlu.framework.R;

import java.util.List;

/**
 *
 * Created by DMing on 2018/1/18.
 */

public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.AlbumViewHolder>{

    public static final int FOLDER = 0;
    public static final int IMAGE = 1;
    private List<AlbumBean> albumBeanList;
    private Context mContext;

    public AlbumAdapter(List<AlbumBean> albumBeanList) {
        this.albumBeanList = albumBeanList;
    }

    @Override
    public AlbumViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        return new AlbumViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_photo,parent,false));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final AlbumViewHolder holder, int position) {
        if (albumBeanList.get(position).getType() == FOLDER) {
            final FolderInfo folderInfo = (FolderInfo) albumBeanList.get(position).getObject();
            holder.mLlFolder.setVisibility(View.VISIBLE);
            holder.mTvCount.setText("" + folderInfo.getSize());
            holder.mTvName.setText(folderInfo.getFolder());
            GlideApp.with(mContext).load(folderInfo.getPath()).placeholder(R.drawable.bg_color_white).centerCrop().into(holder.mIvPic);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null)
                        onItemClickListener.onFolderClick(folderInfo.getImageInfos());
                }
            });
        }else {
            holder.mLlFolder.setVisibility(View.GONE);
            final ImageInfo imageInfo = (ImageInfo) albumBeanList.get(position).getObject();
            GlideApp.with(mContext).load(imageInfo.getPath()).placeholder(R.drawable.bg_color_white).centerCrop().into(holder.mIvPic);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null)
                        onItemClickListener.onSelectImage(imageInfo.getPath());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return albumBeanList.size();
    }

    static class AlbumViewHolder extends RecyclerView.ViewHolder{

        private ImageView mIvPic;
        private TextView mTvCount;
        private TextView mTvName;
        private LinearLayout mLlFolder;

        AlbumViewHolder(View itemView) {
            super(itemView);
            mIvPic = itemView.findViewById(R.id.iv_pic);
            mTvCount = itemView.findViewById(R.id.tv_count);
            mTvName = itemView.findViewById(R.id.tv_name);
            mLlFolder = itemView.findViewById(R.id.ll_folder);
        }
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onFolderClick(List<ImageInfo> imageInfos);
        void onSelectImage(String path);
    }
}
